package com.ruoyi.collect.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.ruoyi.collect.domain.CollectionGroupStats;
import com.ruoyi.collect.domain.RecipeFavorites;
import com.ruoyi.collect.domain.CollectRecipes;
import com.ruoyi.collect.mapper.RecipeFavoritesMapper;
import com.ruoyi.collect.service.IRecipeFavoritesService;
import com.ruoyi.common.utils.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 菜谱收藏Service业务层处理
 * 
 * @author ruoyi
 * @date 2025-08-14
 */
@Service
public class RecipeFavoritesServiceImpl implements IRecipeFavoritesService
{
    @Autowired
    private RecipeFavoritesMapper recipeFavoritesMapper;

    /**
     * 查询菜谱收藏
     * 
     * @param id 菜谱收藏主键
     * @return 菜谱收藏
     */
    @Override
    public RecipeFavorites selectRecipeFavoritesById(String id)
    {
        return recipeFavoritesMapper.selectRecipeFavoritesById(id);
    }

    /**
     * 查询菜谱收藏列表
     * 
     * @param recipeFavorites 菜谱收藏
     * @return 菜谱收藏
     */
    @Override
    public List<RecipeFavorites> selectRecipeFavoritesList(RecipeFavorites recipeFavorites)
    {
        return recipeFavoritesMapper.selectRecipeFavoritesList(recipeFavorites);
    }

    /**
     * 新增菜谱收藏
     * 
     * @param recipeFavorites 菜谱收藏
     * @return 结果
     */
    @Override
    public int insertRecipeFavorites(RecipeFavorites recipeFavorites)
    {
        recipeFavorites.setCreateTime(DateUtils.getNowDate());
        return recipeFavoritesMapper.insertRecipeFavorites(recipeFavorites);
    }

    /**
     * 修改菜谱收藏
     * 
     * @param recipeFavorites 菜谱收藏
     * @return 结果
     */
    @Override
    public int updateRecipeFavorites(RecipeFavorites recipeFavorites)
    {
        return recipeFavoritesMapper.updateRecipeFavorites(recipeFavorites);
    }

    /**
     * 批量删除菜谱收藏
     * 
     * @param ids 需要删除的菜谱收藏主键
     * @return 结果
     */
    @Override
    public int deleteRecipeFavoritesByIds(String[] ids)
    {
        return recipeFavoritesMapper.deleteRecipeFavoritesByIds(ids);
    }

    /**
     * 删除菜谱收藏信息
     * 
     * @param id 菜谱收藏主键
     * @return 结果
     */
    @Override
    public int deleteRecipeFavoritesById(String id)
    {
        return recipeFavoritesMapper.deleteRecipeFavoritesById(id);
    }

    @Override
    public RecipeFavorites findById(String id) {
        return recipeFavoritesMapper.selectById(id);
    }

    @Override
    public Map<String, Object> findByPage(Integer pageNum, Integer pageSize, RecipeFavorites recipeFavorites) {
        PageHelper.startPage(pageNum,pageSize);
        List<RecipeFavorites> list = recipeFavoritesMapper.select(recipeFavorites);
        PageInfo<RecipeFavorites> pageInfo = new PageInfo<>(list);
        Map<String, Object> pageMap = new HashMap<>();
        pageMap.put("total", pageInfo.getTotal());
        pageMap.put("pages", pageInfo.getPages());
        pageMap.put("list", pageInfo.getList());
        return pageMap;
    }

    @Override
    public List<RecipeFavorites> findBatch() {
        return recipeFavoritesMapper.select(null);
    }

    @Override
    public int remove(Integer id) {
        return recipeFavoritesMapper.deleteById(id);
    }

    @Override
    public int removeBatch(Integer[] ids) {
        return recipeFavoritesMapper.delectBatch(ids);
    }

    /**
     * 根据用户ID查询收藏的菜谱列表
     * 
     * @param userId 用户ID
     * @return 菜谱收藏集合（包含菜谱详细信息）
     */
    @Override
    public List<RecipeFavorites> selectRecipeFavoritesByUserId(String userId) {
        return recipeFavoritesMapper.selectRecipeFavoritesByUserId(userId);
    }

    /**
     * 根据用户ID查询收藏夹分组统计信息
     * 通过userId查询collections表获取收藏夹列表，然后统计每个收藏夹中的菜谱数量
     * 
     * @param userId 用户ID
     * @return 收藏夹分组统计列表
     */
    @Override
    public List<CollectionGroupStats> selectCollectionGroupStatsByUserId(String userId) {
        // 1. 通过userId查询collections表获取收藏夹列表
        List<com.ruoyi.collect.domain.Collections> collectionsList = recipeFavoritesMapper.selectCollectionsByUserId(userId);
        
        List<CollectionGroupStats> result = new ArrayList<>();
        
        // 2. 循环每个收藏夹，统计菜谱数量
        for (com.ruoyi.collect.domain.Collections collection : collectionsList) {
            CollectionGroupStats stats = new CollectionGroupStats();
            stats.setCollectionId(collection.getId());
            stats.setCollectionName(collection.getName());
            stats.setCoverUrl(collection.getCoverImage());

            System.out.println(collection);
            
            // 3. 用收藏夹ID查询recipe_favorites表统计数量
            Integer recipeCount = recipeFavoritesMapper.countRecipeFavoritesByCollectionId(collection.getId());
            stats.setRecipeCount(recipeCount);
            
            result.add(stats);
        }
        
        return result;
    }

    /**
     * 根据收藏夹ID统计菜谱收藏数量
     * 
     * @param collectionId 收藏夹ID
     * @return 菜谱数量
     */
    @Override
    public Integer countRecipeFavoritesByCollectionId(String collectionId) {
        return recipeFavoritesMapper.countRecipeFavoritesByCollectionId(collectionId);
    }

    /**
     * 随机返回一条菜谱数据
     * 
     * @return 随机菜谱数据
     */
    @Override
    public CollectRecipes getRandomRecipe() {
        return recipeFavoritesMapper.getRandomRecipe();
    }

    /**
     * 随机返回一条菜谱数据（包含烹饪时间和难度名称）
     * 
     * @return 随机菜谱数据（包含关联信息）
     */
    @Override
    public CollectRecipes getRandomRecipeWithDetails() {
        return recipeFavoritesMapper.getRandomRecipeWithDetails();
    }

    /**
     * 随机返回两条菜谱数据（包含烹饪时间和难度名称）
     * 
     * @return 随机菜谱数据列表（包含关联信息）
     */
    @Override
    public List<CollectRecipes> getRandomRecipesWithDetails() {
        return recipeFavoritesMapper.getRandomRecipesWithDetails();
    }

    /**
     * 根据收藏夹ID查询菜谱列表
     * 
     * @param collectionsId 收藏夹ID
     * @return 菜谱列表
     */
    @Override
    public List<CollectRecipes> getRecipesByCollectionsId(String collectionsId) {
        return recipeFavoritesMapper.getRecipesByCollectionsId(collectionsId);
    }

}
